package com.vitabuybuy.tv.widget.selector;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.vitabuybuy.tv.R;
import com.vitabuybuy.tv.dto.RegionDto;

import java.util.ArrayList;
import java.util.List;


public class Selector implements AdapterView.OnItemClickListener {

    public static final int INDEX_INVALID = -1;
    private final Context context;
    private SelectedListener listener;
    private View view;
    private LinearLayout llTabLayout;
    private TextView tvCancel;
    private TextView tvConfirm;
    private ListView listView;
    private int tabIndex = 0;
    /* 每个tab的adapter */
    private List<List<RegionDto>> allDatas;
    private SelectAdapter[] adapters;
    /*选择的深度*/
    private int selectDeep;
    private int[] selectedIndex;

    DataProvider dataProvider;

    public void setDataProvider(DataProvider dataProvider) {
        this.dataProvider = dataProvider;
        getNextData();
    }

    public Selector(Context context, int deep) {
        this.context = context;
        this.allDatas = new ArrayList<>(deep);
        selectedIndex = new int[deep];
        this.selectDeep = deep;
        for (int i = 0; i < deep; i++) {
            selectedIndex[i] = -1;
            allDatas.add(new ArrayList<>());
        }
        initAdapters();
        initViews();
    }

    private void initAdapters() {
        adapters = new SelectAdapter[allDatas.size()];
        for (int i = 0; i < selectDeep; i++) {
            adapters[i] = new SelectAdapter(allDatas.get(i));
        }
    }

    private TextView[] tabs;

    private void initViews() {
        view = LayoutInflater.from(context).inflate(R.layout.address_selector, null);
        this.listView = (ListView) view.findViewById(R.id.listView);
        this.llTabLayout = (LinearLayout) view.findViewById(R.id.layout_tab);
        this.tvCancel = (TextView) view.findViewById(R.id.tv_cancel);
        this.tvConfirm = (TextView) view.findViewById(R.id.tv_confirm);
        tabs = new TextView[allDatas.size()];
        for (int i = 0; i < allDatas.size(); i++) {
            //动态新增TextView
            TextView textView = (TextView) LayoutInflater.from(context).inflate(R.layout.simple_text_view, llTabLayout, false);
            llTabLayout.addView(textView);
            //绑定TextView的点击事件
            final int finalI = i;
            textView.setOnClickListener(v -> {
                //设置tab 下标
                tabIndex = finalI + 1;
                //更新adapter
                listView.setAdapter(adapters[finalI]);
                //设置选择位置
                if (selectedIndex[finalI] != INDEX_INVALID) {
                    listView.setSelection(selectedIndex[finalI]);
                }
                updateTabsVisibility(tabIndex - 1);
            });
            tabs[i] = textView;
        }

        this.listView.setOnItemClickListener(this);

    }

    public View getView() {
        return view;
    }

    public TextView getTvCancel() {
        return tvCancel;
    }

    public TextView getTvConfirm() {
        return tvConfirm;
    }

    private void updateTabsVisibility(int index) {
        for (int i = 0; i < tabs.length; i++) {
            TextView tv = tabs[i];
            tv.setVisibility(allDatas.get(i).size() != 0 ? View.VISIBLE : View.GONE);
            tv.setEnabled(index != i);
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        this.selectedIndex[tabIndex - 1] = position;
        RegionDto selectAble = allDatas.get(tabIndex - 1).get(position);
        tabs[tabIndex - 1].setText(selectAble.getName());
        for (int i = tabIndex; i < this.allDatas.size(); i++) {
            tabs[i].setText(context.getString(R.string.please_choose));
            allDatas.get(i).clear();
            adapters[i].setSelectedIndex(-1);
            adapters[i].notifyDataSetChanged();
            this.selectedIndex[i] = INDEX_INVALID;
        }
        this.adapters[tabIndex - 1].setSelectedIndex(position);
        this.adapters[tabIndex - 1].notifyDataSetChanged();
//        if (tabIndex == selectDeep) {
//            callbackInternal();
//            return;
//        }
        updateTabsVisibility(tabIndex - 1);
        listener.onClickOnEach(selectAble);
    }

    /**
     * 根据当前集合选择的id，向用户获取下一级子集的数据
     */
    private void getNextData() {
        if (dataProvider == null) {
            return;
        }
        dataProvider.provideData(data -> {
            if (data.size() > 0) {
                //更新当前tab下标
                allDatas.get(tabIndex).clear();
                allDatas.get(tabIndex).addAll(data);
                adapters[tabIndex].notifyDataSetChanged();
                listView.setAdapter(adapters[tabIndex]);
                updateTabsVisibility(tabIndex);
                tabIndex = tabIndex + 1 >= selectDeep ? selectDeep : tabIndex + 1;
            }
        });
    }

    public void callbackInternal() {
        if (listener != null) {
            ArrayList<RegionDto> result = new ArrayList<>();
            for (int i = 0; i < selectDeep; i++) {
                RegionDto resultBean = allDatas.get(i) == null
                        || selectedIndex[i] == INDEX_INVALID ? null : allDatas.get(i).get(selectedIndex[i]);
                if (resultBean != null) {
                    result.add(resultBean);
                }
            }
            if(result.size() == 0){
//                XToast.normal(context.getString(R.string.please_choose_coutry));
                return;
            }
            listener.onAddressSelected(result);
        }
    }


    public void setSelectedListener(SelectedListener listener) {
        this.listener = listener;
    }
}
